package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.ModulesShopsBO;
import com.junzhiit.nengji.cms.service.ModulesShopsService;
import com.junzhiit.nengji.cms.web.exception.ModulesNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ModulesShopsVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.ModulesDAO;
import com.junzhiit.nengji.dao.ModulesShopsDAO;
import com.junzhiit.nengji.model.Modules;
import com.junzhiit.nengji.model.ModulesShops;

import cn.hutool.core.bean.BeanUtil;

@Service
public class ModulesShopsServiceImpl extends BaseServiceImpl implements ModulesShopsService {

	@Autowired
	ModulesShopsDAO modulesShopsDAO;
	
	@Autowired
	ModulesDAO modulesDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,ModulesShopsVO msv) {
		ModulesShopsBO msb=new ModulesShopsBO();
		BeanUtil.copyProperties(msv, msb);
		msb.setIsOpenPage(0);
		List<ModulesShopsBO> msbList=modulesShopsDAO.findAll(dtp.getStart(),dtp.getLength(),msb);
		Integer totalCount=modulesShopsDAO.getTotalCountByAll(msb);
		return ViewObjectUtil.dataTables(msbList, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO addModulesShop(ModulesShopsVO msv) {
		ResultVO result=new ResultVO();
		Modules m=modulesDAO.query(msv.getModulesId());
		if(m==null){
			throw new ModulesNotFoundException("模块不存在");
		}
		ModulesShopsBO msb=new ModulesShopsBO();
		BeanUtil.copyProperties(msv, msb);
		Integer maxSort=modulesShopsDAO.getMaxSort(msb);
		msb.setSort(maxSort+1);
		ModulesShops ms=new ModulesShops();
		BeanUtil.copyProperties(msb, ms);
		modulesShopsDAO.insert(ms);
		return result;
	}

	@Override
	public ResultVO delModulesShop(ModulesShopsVO msv) {
		ResultVO result=new ResultVO();
		Modules m=modulesDAO.query(msv.getModulesId());
		if(m==null){
			throw new ModulesNotFoundException("模块不存在");
		}
		ModulesShopsBO msb=new ModulesShopsBO();
		BeanUtil.copyProperties(msv, msb);
		modulesShopsDAO.deleteByCsId(msb);
		return result;
	}

}
